namespace C1.Win.C1FlexGrid
{
    using C1.Util;
    using C1.Win.C1FlexGrid.Util.BaseControls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    internal class GridEditorTextBox : MaskedTextBox, IGridEditor
    {
        internal int _col;
        internal bool _fireEvents;
        internal Point _offset;
        internal int _row;
        internal C1FlexGridBase _view;

        internal GridEditorTextBox(C1FlexGridBase view)
        {
            this._view = view;
            this._row = this._col = -1;
            this._offset = new Point(0, 0);
        }

        public object GetContent()
        {
            return base.GetValidContent();
        }

        private void InitEditor(int row, int col)
        {
            bool flag;
            this._row = row;
            this._col = col;
            this._view.ShowCell(row, col);
            this._fireEvents = false;
            CellStyle cellStyleDisplay = this._view.GetCellStyleDisplay(row, col);
            this.Font = cellStyleDisplay.Font;
            this.BackColor = cellStyleDisplay.BackColor;
            this.ForeColor = cellStyleDisplay.ForeColor;
            base.BorderStyle = BorderStyle.None;
            base.AcceptsReturn = flag = cellStyleDisplay.WordWrap;
            base.WordWrap = this.Multiline = flag;
            string dataDisplay = this._view._grid.GetDataDisplay(row, col);
            base.TextAlign = cellStyleDisplay.GetEditorAlign(dataDisplay);
            base.Mask = this._view._grid.GetCellEditMask(row, col);
            base.AcceptsTab = this._view.KeyActionTab != KeyActionEnum.None;
            this.Text = string.Empty;
            this.UpdatePosition();
            this._view.OnSetupEditor(row, col);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            e.Handled = this._view.OnKeyDownEdit(this._row, this._col, e);
            if (!e.Handled)
            {
                base.OnKeyDown(e);
                if (!e.Handled)
                {
                    bool flag = false;
                    bool cancel = false;
                    bool flag3 = true;
                    switch (e.KeyCode)
                    {
                        case Keys.Prior:
                        case Keys.Next:
                        case Keys.Tab:
                            flag = true;
                            break;

                        case Keys.Left:
                            if ((base.SelectionStart == 0) && (this.SelectionLength == 0))
                            {
                                flag = true;
                            }
                            break;

                        case Keys.Up:
                            flag = true;
                            break;

                        case Keys.Right:
                            if (base.SelectionStart > (this.TextLength - 1))
                            {
                                flag = true;
                            }
                            break;

                        case Keys.Down:
                            flag = true;
                            break;

                        case Keys.Escape:
                            flag = cancel = true;
                            flag3 = false;
                            break;

                        case Keys.Return:
                            if (!e.Shift && !e.Control)
                            {
                                flag = true;
                            }
                            break;
                    }
                    if (flag)
                    {
                        e.Handled = true;
                        if (this._view.FinishEditing(cancel) && flag3)
                        {
                            this._view._kbdHandler.HandleEditorKey(e);
                        }
                    }
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            e.Handled = this._view.OnKeyPressEdit(this._row, this._col, e);
            if (!e.Handled)
            {
                base.OnKeyPress(e);
                if (!e.Handled)
                {
                    if (((e.KeyChar == '\r') || (e.KeyChar == '\x001b')) || (e.KeyChar == '\t'))
                    {
                        e.Handled = true;
                    }
                    if (e.KeyChar == '☻')
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            e.Handled = this._view.OnKeyUpEdit(this._row, this._col, e);
            if (!e.Handled)
            {
                base.OnKeyUp(e);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (this._fireEvents && !base.IsUpdating())
            {
                this._fireEvents = false;
                this._view.OnChangeEdit();
                this._fireEvents = true;
            }
        }

        public void StartEditing(int row, int col, char key)
        {
            this.InitEditor(row, col);
            base.Visible = true;
            base.Focus();
            if (key <= ' ')
            {
                object data = this._view._grid.GetData(row, col);
                string str = (data == null) ? string.Empty : data.ToString();
                this.Text = str;
                int length = str.Length;
                if (key == ' ')
                {
                    base.Select(length, 0);
                }
                else
                {
                    base.Select(0, length);
                }
            }
            else
            {
                if (this._view.OnKeyPressEdit(this._row, this._col, new KeyPressEventArgs(key)))
                {
                    this._view.FinishEditing(true);
                    return;
                }
                int num2 = base.EditPosition(0);
                if (num2 <= this.Text.Length)
                {
                    this.Text = (this.Text.Length > 0) ? (this.Text.Substring(0, num2) + key + this.Text.Substring(num2 + 1)) : key.ToString();
                    if ((num2 < this.Text.Length) && (char.ToLower(this.Text[num2]) == char.ToLower(key)))
                    {
                        num2 = base.EditPosition(1);
                    }
                    base.Select(num2, 0);
                }
            }
            this._fireEvents = true;
            this._view.OnChangeEdit();
        }

        public void StartEditing(int row, int col, Point pt, bool dblClick)
        {
            this.InitEditor(row, col);
            object data = this._view._grid.GetData(row, col);
            this.Text = (data == null) ? string.Empty : data.ToString();
            base.Visible = true;
            base.Focus();
            this._fireEvents = true;
            this._view.OnChangeEdit();
        }

        public void UpdatePosition()
        {
            Rectangle cellRectEditor = this._view.GetCellRectEditor(this._row, this._col);
            int right = cellRectEditor.Right;
            CellStyle cellStyleDisplay = this._view.GetCellStyleDisplay(this._row, this._col);
            cellRectEditor = cellStyleDisplay.GetContentRectangle(cellRectEditor);
            if (this._view.EditHandler.GetEditorType(this._row, this._col) == EditorTypeEnum.PopEdit)
            {
                int num2 = (right - cellRectEditor.X) - C1.Util.SystemInformation.VerticalScrollBarWidth;
                cellRectEditor.Width = num2;
            }
            base.Bounds = cellRectEditor;
            this._offset.X = base.Bounds.X + cellStyleDisplay.Margins.Left;
            this._offset.Y = base.Bounds.Y + cellStyleDisplay.Margins.Top;
        }
    }
}

